package com.ly.lypopupwindowdemo.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.adapter.MyAdapter;
import com.ly.lypopupwindowdemo.decoration.ColorDivederDecoration;
import com.ly.lypopupwindowdemo.decoration.DividerGridItemDecoration;
import com.ly.lypopupwindowdemo.decoration.GridDivider;
import com.ly.lypopupwindowdemo.decoration.SimpleDividerDecoration;
import com.ly.lypopupwindowdemo.listener.RecyclerViewClickListener;
import com.ly.lypopupwindowdemo.listener.RecyclerViewClickListener2;

import java.util.ArrayList;
import java.util.List;

import static android.widget.LinearLayout.HORIZONTAL;

/**
 * 1.1 构造方法
 * <p>
 * 有3个构造方法：
 * <p>
 * LinearLayoutManager(Context context)
 * LinearLayoutManager(Context context,int orientation,boolean reverseLayout)
 * LinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr,int defStyleRes)
 * 第一个构造方法内部调用了第二个构造方法，第二个构造方法参数的含义：
 * <p>
 * Context context ：上下文，初始化时，构造方法内部加载资源用
 * int orientation ：方向，垂直和水平，默认为垂直
 * boolean reverseLayout：是否倒序，设置为True，从最后一个item开始，倒序加载。
 * 此时，RecyclerView第一个item是添加进Adapter中的最后一个，最后一个item是第一个加进Adapter的数据,RecyclerView会自动滑到末尾
 * <p>
 * 链接：http://www.jianshu.com/p/8e578d8ebe5f
 * 來源：简书
 * 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
 *
 *
 * RecyclerView继续学习
 * http://mp.weixin.qq.com/s/LsxYOGnp6Yq_9rFHUDAL9A
 */

public class NewRecycleViewActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private MyAdapter mAdapter;
    private List<String> mData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_recycle_view);

        initData();
        initRecyclerView();
    }

    private void initRecyclerView() {
        //1 实例化RecyclerView
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        //mLayoutManager = new LinearLayoutManager(this,HORIZONTAL,false);
        mLayoutManager = new LinearLayoutManager(this);
        //mLayoutManager = new GridLayoutManager(this, 4);


        //2 为RecyclerView创建布局管理器，这里使用的是LinearLayoutManager，表示里面的Item排列是线性排列
        mRecyclerView.setLayoutManager(mLayoutManager);

        /**
         * 其中第二个参数spanCount表示表格的行数或者列数；
         * 第三个参数表示是水平滑动或者是竖直方向滑动；最后一个参数表示是否从数据的尾部开始显示。
         */
        //mRecyclerView.setLayoutManager(new GridLayoutManager(this, 4, VERTICAL, false));


        //构造器中，第一个参数表示列数或者行数，第二个参数表示滑动方向
        //mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));

        mAdapter = new MyAdapter(mData);

        mRecyclerView.addItemDecoration(new ColorDivederDecoration());

       // mRecyclerView.addItemDecoration(new SimpleDividerDecoration(this));

        // mRecyclerView.addItemDecoration(new SimplePaddingDecoration(this));
        // mRecyclerView.addItemDecoration(new DividerItemDecoratoin(this, VERTICAL));

       // mRecyclerView.addItemDecoration(new DividerGridItemDecoration(this));

      // mRecyclerView.addItemDecoration(new GridDivider(this, 100, this.getResources().getColor(R.color.colorAccent)));


        //3 设置数据适配器
        mRecyclerView.setAdapter(mAdapter);

        /**
         * 方法二
         */
//        mRecyclerView.addOnItemTouchListener(new RecyclerViewClickListener(this, new RecyclerViewClickListener.OnItemClickListener() {
//            @Override
//            public void onItemClick(View view, int position) {
//                Toast.makeText(NewRecycleViewActivity.this, "click=="+position, Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onItemLongClick(View view, int position) {
//
//                Toast.makeText(NewRecycleViewActivity.this, "longClick="+position, Toast.LENGTH_SHORT).show();
//            }
//        }));
        /**
         * 方法三
         */

        mRecyclerView.addOnItemTouchListener(new RecyclerViewClickListener2(this, mRecyclerView,
                new RecyclerViewClickListener2.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                         Toast.makeText(NewRecycleViewActivity.this,"Click "+mData.get(position),Toast.LENGTH_SHORT).show();
                        //点击添加
                        mAdapter.addData(position);
                        //mAdapter.changeData(position);
                    }

                    @Override
                    public void onItemLongClick(View view, int position) {
                        Toast.makeText(NewRecycleViewActivity.this,"Long Click "+mData.get(position),Toast.LENGTH_SHORT).show();
                        //长按移除item
                        mAdapter.removeData(position);
                    }
                }));


    }

    private void initData() {
        mData = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            mData.add("Item " + i);
        }
    }
}
